﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PersonalExpenseTracker
{
    public partial class ExpenseTracker : Form
    {
        bool blnClose = false;

        public ExpenseTracker()
        {
            InitializeComponent();
            this.HelpButton = true;
            this.notifyIcon1.Visible = Configuration.ConfigurationSettings.AppSettings.AlwaysShowTrayIcon;
        }

        private void tsbSummary_Click(object sender, EventArgs e)
        {
            SetTitle("Summary");
            DisplayUserControl(new SummaryUserControl());
        }

        private void tsbReports_Click(object sender, EventArgs e)
        {
            SetTitle("Reports");
            DisplayUserControl(new ReportsUserControl());
        }

        private void tsbExpenseList_Click(object sender, EventArgs e)
        {
            SetTitle("Expense List");
            DisplayUserControl(new ExpenseListUserControl());
        }

        private void tsbCategory_Click(object sender, EventArgs e)
        {
            SetTitle("Category Management");
            DisplayUserControl(new CategoryUserControl());
        }

        private void tsbNew_Click(object sender, EventArgs e)
        {
            SetTitle("New Expense");
            DisplayUserControl(new ExpenseUserControl());
        }

        private void tsbPreferences_Click(object sender, EventArgs e)
        {
            SetTitle("Preferences");
            DisplayUserControl(new PreferencesUserControl());
        }

        private void DisplayUserControl(UserControl uc)
        {
            this.Cursor = Cursors.WaitCursor;
            this.SuspendLayout();
            this.pnlMain.SuspendLayout();
            uc.Width = pnlMain.Width;
            uc.Height = pnlMain.Height;
            if (pnlMain.Controls.Count == 1)
            {
                Control control = pnlMain.Controls[0];
                control.Visible = false;
                pnlMain.Controls.Clear();
                control.Dispose();
                control = null;
            }
            pnlMain.Controls.Add(uc);
            uc.Dock = DockStyle.Fill;
            uc.Show();
            uc.Focus();
            this.pnlMain.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();
            this.Cursor = Cursors.Default;
        }

        private void tsbHelp_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                System.Diagnostics.Process.Start(
                    Application.StartupPath + "\\Help\\index.htm");
            }
            catch
            {
            }
            this.Cursor = Cursors.Default;
        }
        
        private void tsbAbout_Click(object sender, EventArgs e)
        {
            using (About frmAbout = new About())
            {
                frmAbout.ShowDialog();
            }
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            blnClose = true;
            this.notifyIcon1.Visible = false;
            Application.Exit();
        }

        private void ExpenseTracker_Load(object sender, EventArgs e)
        {
            tsbSummary_Click(sender, e);
        }

        private void SetTitle(string title)
        {
            this.Text = "Personal Expense Tracker - " + title;
        }

        public void EditExpense(PersonalExpenseTracker.PersistenceLayer.Expense exp)
        {
            SetTitle("New Expense");
            ExpenseUserControl uc = new ExpenseUserControl(exp);
            DisplayUserControl(uc);
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            if (Configuration.ConfigurationSettings.AppSettings.SingleClickOnTrayToOpen)
            {
                notifyIcon1_DoubleClick(sender, e);
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            // Show the form when the user double clicks on the notify icon.
            this.notifyIcon1.Visible = Configuration.ConfigurationSettings.AppSettings.AlwaysShowTrayIcon;

            // Activate the form.
            this.Show();
            // Set the WindowState to maximized if the form is minimized.
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;

            this.Activate();
        }

        private void ExpenseTracker_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                if (Configuration.ConfigurationSettings.AppSettings.MinimizeToTray)
                {
                    this.Hide();
                    this.notifyIcon1.Visible = true;
                }
            }
        }

        private void ExpenseTracker_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Configuration.ConfigurationSettings.AppSettings.CloseToTray&& !blnClose)
            {
                e.Cancel = true;
                this.Hide();
                this.notifyIcon1.Visible = true;
            }
            else
            {
                this.notifyIcon1.Visible = false;
            }
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            tsbExit_Click(sender, e);
        }

    }
}
